package com.kye.express.business.deposit.viewholder;

import android.databinding.DataBindingUtil;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.jakewharton.rxbinding2.view.RxView;
import com.kye.base.DebounceObservableTransformer;
import com.kye.express.R;
import com.kye.express.business.deposit.model.DepositConsumptionDateModel;
import com.kye.express.databinding.ItemDepositConsumptionDateBinding;

import me.drakeet.multitype.ItemViewBinder;

/**
 * 预存运费-消费记录 日期选择器 ViewHolder
 * <p>
 * Created by SkyYu on 2018/1/24
 */
public class DepositConsumptionDateViewBinder extends ItemViewBinder<DepositConsumptionDateModel, DepositConsumptionDateViewBinder.ViewHolder> {

    private OnTabLayoutListener onTabLayoutListener;

    public DepositConsumptionDateViewBinder(OnTabLayoutListener onTabLayoutListener) {
        this.onTabLayoutListener = onTabLayoutListener;
    }

    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View view = inflater.inflate(R.layout.item_deposit_consumption_date, parent, false);
        return new ViewHolder(view);
    }

    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, @NonNull DepositConsumptionDateModel item) {
        holder.bindData(item);
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        private DepositConsumptionDateModel data;
        private ItemDepositConsumptionDateBinding binding;

        public ViewHolder(final View itemView) {
            super(itemView);
            binding = DataBindingUtil.bind(itemView);
            RxView.clicks(binding.linearlayoutDepositConsumptionDateFrom)
                    .compose(new DebounceObservableTransformer<>())
                    .subscribe((Object) -> {
                        if (onTabLayoutListener != null) {
                            onTabLayoutListener.onSelectDateFrom(data);
                        }
                    });
            RxView.clicks(binding.linearlayoutDepositConsumptionDateTo)
                    .compose(new DebounceObservableTransformer<>())
                    .subscribe((Object) -> {
                        if (onTabLayoutListener != null) {
                            onTabLayoutListener.onSelectDateTo(data);
                        }
                    });
            RxView.clicks(binding.linearlayoutDepositConsumptionQuery)
                    .compose(new DebounceObservableTransformer<>())
                    .subscribe((Object) -> {
                        if (onTabLayoutListener != null) {
                            onTabLayoutListener.onQueryConsumptionObtainData(data);
                        }
                    });
        }

        public void bindData(DepositConsumptionDateModel item) {
            data = item;
            binding.setData(data);
        }
    }

    public interface OnTabLayoutListener {
        void onSelectDateFrom(DepositConsumptionDateModel item);

        void onSelectDateTo(DepositConsumptionDateModel item);

        void onQueryConsumptionObtainData(DepositConsumptionDateModel item);
    }
}